#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

// newcode: DP41 【模板】01背包
//https://www.nowcoder.com/share/jump/8579736501700990542937
int n, V;

int request1(vector<int>& v, vector<int>& w)
{
    // vector<vector<int>> dp(n+1, vector<int>(V+1));

    // for (int i = 1; i<=n; ++i)
    // {
    //     for (int j = 1; j<=V; ++j)
    //     {
    //         dp[i][j] = dp[i-1][j];
    //         if (j >= v[i])
    //             dp[i][j] = max(dp[i][j], w[i]+dp[i-1][j-v[i]]);
    //     }
    // }

    // return dp[n][V];

    // 空间优化：
    vector<int> dp(V+1);
    for (int i = 1; i<=n; ++i)
    {
        for (int j = V; j >= v[i]; --j)
        {
            dp[j] = max(dp[j], w[i] + dp[j-v[i]]);
        }
    }

    return dp[V];
}

int request2(vector<int>& v, vector<int>& w)
{
    // vector<vector<int>> dp(n+1, vector<int>(V+1));
    
    // for (int j = 1; j<=V; ++j)
    //     dp[0][j] = -1;

    // for (int i = 1; i<=n; ++i)
    // {
    //     for (int j = 1; j<=V; ++j)
    //     {
    //         dp[i][j] = dp[i-1][j];
    //         if (j>=v[i] && dp[i-1][j-v[i]] >= 0)
    //             dp[i][j] = max(dp[i][j], w[i]+dp[i-1][j-v[i]]);
    //     }
    // }

    // return dp[n][V] >= 0 ? dp[n][V] : 0;

    // 空间优化：
    vector<int> dp(V+1);
    
    for (int j = 1; j<=V; ++j)
        dp[j] = -1;

    for (int i = 1; i<=n; ++i)
    {
        for (int j = V; j >= v[i]; --j)
        {
            if (dp[j-v[i]] >= 0)
                dp[j] = max(dp[j], w[i]+dp[j-v[i]]);
        }
    }

    return dp[V] >= 0 ? dp[V] : 0;
}


int main()
{
    cin >> n >> V;
    vector<int> v(n+1), w(n+1);

    for (int i = 1; i<=n; ++i)
    {
        cin >> v[i] >> w[i];
    }

    cout << request1(v, w) << endl;
    cout << request2(v, w) << endl;

    return 0;
}